<template>
  <el-dialog title="新增员工" :visible="showDialog" :destroy-on-close="true" @close="closeFn">
    <el-form ref="addForm" :model="form" :rules="rules">
      <el-form-item label="姓名" label-width="120px" prop="username">
        <el-input v-model="form.username" style="width:90%" />
      </el-form-item>
      <el-form-item label="手机" label-width="120px" prop="mobile">
        <el-input v-model="form.mobile" style="width:90%" />
      </el-form-item>
      <el-form-item label="入职时间" label-width="120px" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          type="date"
          placeholder="请选择入职时间"
          style="width:90%"
        />
      </el-form-item>
      <el-form-item label="聘用形式" label-width="120px" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择" style="width:90%">
          <el-option v-for="item in EnumEmployee.hireType" :key="item.id" :value="item.id" :label="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" label-width="120px" prop="workNumber">
        <el-input v-model="form.workNumber" style="width:90%" />
      </el-form-item>
      <el-form-item label="部门" label-width="120px" prop="departmentName">
        <el-input v-model="form.departmentName" style="width:90%" @focus="showList=true" />
        <el-tree
          v-if="showList"
          class="departmentTree"
          :data="departmentList"
          :props="{label:'name'}"
          default-expand-all
          @node-click="nodeClick"
        />
      </el-form-item>
      <el-form-item label="转正时间" label-width="120px" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          type="date"
          placeholder="请选择入职时间"
          style="width:90%"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeFn">取 消</el-button>
      <el-button type="primary" @click="btnOk">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import EnumEmployee from '@/api/constant/employees'
import { getDepartment } from '@/api/department'
import { listToTreeList } from '@/utils'
import { addEmployee } from '@/api/employee'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EnumEmployee,
      departmentList: [], // 部门树形表
      showList: false, // 部门下拉表显示
      form: {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        timeOfEntry: '', // 入职时间
        correctionTime: ''// 转正时间
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名长度为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '组织名称不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    //   获取部门树形结构
    const { depts } = await getDepartment()
    this.departmentList = listToTreeList(depts, '')
  },
  methods: {
    // 关闭弹窗
    closeFn() {
      this.form = {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        timeOfEntry: '', // 入职时间
        correctionTime: ''// 转正时间
      }
      // form组件自带的方法，重置表单，清空校验
      this.$refs.addForm.resetFields()
      this.$emit('update:show-dialog', false)
    },
    // 添加按钮
    async btnOk() {
      await this.$refs.addForm.validate()
      await addEmployee(this.form)
      this.$message.success('新增成功')
      this.closeFn()
      this.$emit('addSuccess')
    },
    // 点击部门选项
    nodeClick(data) {
      this.form.departmentName = data.name
      this.showList = false
    }
  }
}
</script>

<style lang="scss" scoped>
.departmentTree{
    overflow: auto;
    position: absolute;
    width: 90%;
    height: 200px;
    z-index: 9;
    border: 1px solid #ccc;
}
</style>
